<template>
  <div class="home">
    <van-row>
      <van-col class="top" span="24"></van-col>
      <van-field label="输入用户名：" style="padding-top:30px" v-model="username" />

      <van-col class="btn-box" span="24">
        <van-button
          :disabled="!btnActive"
          :plain="!btnActive"
          @click="toLogin"
          class="btn-item"
          round
          type="info"
        >登录</van-button>
      </van-col>
    </van-row>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      username: '',
    };
  },
  computed: {
    btnActive() {
      return this.username.length > 0;
    },
  },
  methods: {
    toLogin() {
      this.initChat()
      this.$router.push('/chat');
    },
    initChat() {
      let userInfo = {
        userId: Date.now(),
        username: this.username,
      };
      //登录之后开始连接ws
      window.socket = window.io.connect('ws://localhost:3000/');
      window.socket.emit('login', userInfo);
      window.userInfo = userInfo
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  background-color: #ffffff;

  .top {
    width: 100%;
    height: 260px;
    background: url('~@/assets/bg.jpg') no-repeat;
    background-size: cover;

    .logo {
      height: 100px;
      margin-top: 140px;
      line-height: 100px;
      color: #ffffff;
      text-align: center;
    }
  }

  .btn-box {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    height: 90px;
    line-height: 90px;
    text-align: center;

    .btn-item {
      width: 55%;
    }
  }
}
</style>
